--[[
	AtlasLootReverse
	Written by pceric
	http://www.wowinterface.com/downloads/info9179-Know-It-All.html
]]
AtlasLootReverse = LibStub("AceAddon-3.0"):NewAddon("AtlasLootReverse", "AceConsole-3.0")
local TT = LibStub("LibExtraTip-1")
local L = LibStub("AceLocale-3.0"):GetLocale("AtlasLootReverse", true)

AtlasLootReverse.title = L["AtlasLootReverse"]
AtlasLootReverse.version = GetAddOnMetadata("AtlasLootReverse", "Version")

local db
local tmp = ""
local excludes = {
	[29434] = true,  -- BoJ
	[40752] = true,  -- EoH
	[40753] = true,  -- EoV
}

-- Default settings
local defaults = {
	profile = {
		dbversion = "",
		alversion = "",
		whoTable = {},
		embedded = true
	}
}

-- searches a table for string s
local function tfind(t, s)
	local last
	for k, v in pairs(t) do
		if type(v) == "table" then
			tfind(v, s)
		else
			if v == s then tmp = last end
			last = v
		end
	end 
end

----------------------
--On start
function AtlasLootReverse:OnInitialize()
	local alrdb = LibStub("AceDB-3.0"):New("AtlasLootReverseDB", defaults)
	db = alrdb.profile --local the profile table
    self:RegisterChatCommand(L["atlaslootreverse"], "ChatCommand")
    self:RegisterChatCommand(L["alr"], "ChatCommand")
	TT:RegisterTooltip(GameTooltip)
	TT:RegisterTooltip(ItemRefTooltip)
end

----------------------
--Disabled
function AtlasLootReverse:OnDisable()
	TT:RemoveCallback(AtlasLootReverse.Who)
end

----------------------
--Loaded
function AtlasLootReverse:OnEnable()
	if db.dbversion ~= AtlasLootReverse.version or db.alversion ~= ATLASLOOT_VERSION then
		local zone
		wipe(db.whoTable) -- Wipe the table
		AtlasLoot_LoadAllModules()  -- Force AtlasLoot to load all modules
		for k1,v1 in pairs(AtlasLoot_TableNames) do
			if type(AtlasLoot_Data[k1]) == "table" then
				for k2,v2 in pairs(AtlasLoot_Data[k1]) do
					if type(v2) == "table" and type(v2[2]) == "number" and v2[2] > 0 and not excludes[v2[2]] and v1[1] ~= "Keys" then
						if strsub(k1, -5) == "25Man" then
							zone = strtrim(string.format("25 人 %s", AtlasLootReverse:FindZone(strsub(k1, 1, strlen(k1)-5))))
						else
							zone = AtlasLootReverse:FindZone(k1)
						end
						if zone ~= "" then zone = " (" .. zone .. ")" end
						if (db.whoTable[v2[2]]) then
							if not string.find(db.whoTable[v2[2]], v1[1] .. zone, 1, true) then
								db.whoTable[v2[2]] = db.whoTable[v2[2]] .. "|" .. v1[1] .. zone
							end
						else
							db.whoTable[v2[2]] = v1[1] .. zone
						end
					end
				end
			end
		end
		db.dbversion = AtlasLootReverse.version
		db.alversion = ATLASLOOT_VERSION
		print(AtlasLootReverse.title .. " database rebuilt.")
	end
	TT:AddCallback(AtlasLootReverse.Who)
end

----------------------
--Command Handler
function AtlasLootReverse:ChatCommand(input)
    if input:trim() == "embed" then
        db.embedded = not db.embedded
		print("Tooltip embedded: " .. tostring(db.embedded))
	else
		print("Commands:\n/alr embed - Toggles the tooltip being embedded or split into another box")
    end
end

----------------------
--Gets a Zone given an AtlasLoot Boss ID
function AtlasLootReverse:FindZone(ALBID)
	local val
	for k,v in pairs(AtlasLoot_DewDropDown_SubTables) do
		for i,j in pairs(v) do
			if j[2] == ALBID then
				tfind(AtlasLoot_DewDropDown, k)
				val = tmp
				tmp = ""
				return val
			end
		end
	end
	return ""
end

-- LibExtraTip Call Backs
function AtlasLootReverse:Who(item)
	local _, itemId = strsplit(":", item)
	--TT:AddLine(self, itemId, nil, nil, nil, db.embedded)
	if db.whoTable[tonumber(itemId)] then
		for v in string.gmatch(db.whoTable[tonumber(itemId)], "[^|]+") do
			if not string.find(v, 'Tier ') and not string.find(v, 'Tabards ') and not string.find(v, 'PvP ') then
				v = string.format("来源： %s", v)
			end
			TT:AddLine(self, v, nil, nil, nil, db.embedded)
		end
	end
end